/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huawei.saas.config.balancestrategy;

import com.huawei.saas.config.dynamicdatasource.SnapshotDataSource;
import com.huawei.saas.constants.LoadBalanceStrategyEnum;

import java.security.SecureRandom;
import java.util.List;
/**
 * 随机算法
 *
 * @author lWX1156935
 * @since 2022/4/22
 */
public class RandomLoadBalanceStrategy implements LoadBalanceStrategy {
    @Override
    public String getStrategyType() {
        return LoadBalanceStrategyEnum.RANDOM.getCode();
    }

    @Override
    public SnapshotDataSource get(List<SnapshotDataSource> dataSourceList) {
        if (dataSourceList.size() == 1) {
            return dataSourceList.get(0);
        }
        return dataSourceList.get(new SecureRandom().nextInt(dataSourceList.size()));
    }
}
